from manger import db
from marshmallow import Schema, fields, post_load

class PositonStock(db.Model):
    __tablename__ = 'position_stocks'
    ID = db.Column(db.Integer, primary_key=True)
    code = db.Column(db.String, primary_key=False)
    creatTime = db.Column(db.DateTime, nullable=False)
    diff = db.Column(db.Float, nullable=False)
    name = db.Column(db.String, nullable=False)
    nowCost15 = db.Column(db.Float, nullable=False)
    nowCost30 = db.Column(db.Float, nullable=False)
    score = db.Column(db.Float, nullable=False)
    Topics = db.Column(db.String, nullable=False)
    TopicsHot = db.Column(db.String, nullable=False)
    kplTopics = db.Column(db.String, nullable=False)
    isSelected = db.Column(db.Integer, nullable=False, default=0)
    defensive15Time = db.Column(db.Float, nullable=False)
    defensive30Time = db.Column(db.Float, nullable=False)
    defensive15 = db.Column(db.DateTime, nullable=False)
    defensive30 = db.Column(db.DateTime, nullable=False)
    isFall15 = db.Column(db.Integer, nullable=True, default=0)
    isFall30 = db.Column(db.Integer, nullable=False, default=0)
    todayIsFall15 = db.Column(db.Integer, nullable=False, default=0)
    todayIsFall30 = db.Column(db.Integer, nullable=False, default=0)


class PositonStockSchema(Schema):
    ID = fields.Integer()
    code = fields.String()
    creatTime = fields.DateTime()
    diff = fields.Float()
    name = fields.String()
    nowCost15 = fields.Float()
    nowCost30 = fields.Float()
    score = fields.Float()
    Topics = fields.String()
    TopicsHot = fields.String()
    kplTopics = fields.String()
    isSelected = fields.Integer()
    defensive15Time = fields.DateTime()
    defensive30Time = fields.DateTime()
    defensive15 = fields.Float()
    defensive30 = fields.Float()
    isFall15 = fields.Integer()
    isFall30 = fields.Integer()
    todayIsFall15 = fields.Integer()
    todayIsFall30 = fields.Integer()
    @post_load  # 反序列化成一个对象，给schema的一个方法加上post_load装饰器
    def post_load(self, data, **kwargs):
        return PositonStock(**data)